home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / dflat_r_.arc / CONSOLE.C < prev    next >
Text File  |  1991-10-02  |  6KB  |  284 lines

  1. /* ----------- console.c ---------- */
  2.  
  3. #include <conio.h>
  4. #include <bios.h>
  5. #include <dos.h>
  6. #include "system.h"
  7. #include "keys.h"
  8.  
  9. /* ----- table of alt keys for finding shortcut keys ----- */
  10. static int altconvert[] = {
  11.     ALT_A,ALT_B,ALT_C,ALT_D,ALT_E,ALT_F,ALT_G,ALT_H,
  12.     ALT_I,ALT_J,ALT_K,ALT_L,ALT_M,ALT_N,ALT_O,ALT_P,
  13.     ALT_Q,ALT_R,ALT_S,ALT_T,ALT_U,ALT_V,ALT_W,ALT_X,
  14.     ALT_Y,ALT_Z,ALT_0,ALT_1,ALT_2,ALT_3,ALT_4,ALT_5,
  15.     ALT_6,ALT_7,ALT_8,ALT_9
  16. };
  17.  
  18. unsigned video_mode;
  19. unsigned video_page;
  20.  
  21. static int near cursorpos[MAXSAVES];
  22. static int near cursorshape[MAXSAVES];
  23. static int cs;
  24.  
  25. static union REGS regs;
  26.  
  27. void initvideo(void)
  28. {
  29.     if (isEGA() || isVGA())    {
  30.         regs.x.ax = 0x1003;
  31.         regs.h.bl = 0;
  32.         int86(VIDEO, ®s, ®s);
  33.     }
  34. }
  35.  
  36. void restorevideo(void)
  37. {
  38.     if (isEGA() || isVGA())    {
  39.         regs.x.ax = 0x1003;
  40.         regs.h.bl = 1;
  41.         int86(VIDEO, ®s, ®s);
  42.     }
  43. }
  44.  
  45. void SwapCursorStack(void)
  46. {
  47.     if (cs > 1)    {
  48.         swap(cursorpos[cs-2], cursorpos[cs-1]);
  49.         swap(cursorshape[cs-2], cursorshape[cs-1]);
  50.     }
  51. }
  52.  
  53. #ifndef MSC
  54. #ifndef WATCOM
  55. #define ZEROFLAG 0x40
  56. /* ---- Test for keystroke ---- */
  57. int keyhit(void)
  58. {
  59.     _AH = 1;
  60.     geninterrupt(KEYBRD);
  61.     return (_FLAGS & ZEROFLAG) == 0;
  62. }
  63. #endif
  64. #endif
  65.  
  66. /* ---- Read a keystroke ---- */
  67. int getkey(void)
  68. {
  69.     int c;
  70.     while (keyhit() == 0)
  71.         ;
  72.     if (((c = bioskey(0)) & 0xff) == 0)
  73.         c = (c >> 8) | 0x1080;
  74.     else
  75.         c &= 0xff;
  76.     return c & 0x10ff;
  77. }
  78.  
  79. /* ---------- read the keyboard shift status --------- */
  80. int getshift(void)
  81. {
  82.     regs.h.ah = 2;
  83.     int86(KEYBRD, ®s, ®s);
  84.     return regs.h.al;
  85. }
  86.  
  87. static int far *clk = MK_FP(0x40,0x6c);
  88. /* ------- macro to wait one clock tick -------- */
  89. #define wait()          \
  90. {                       \
  91.     int now = *clk;     \
  92.     while (now == *clk) \
  93.         ;               \
  94. }
  95.  
  96. /* -------- sound a buzz tone ---------- */
  97. void beep(void)
  98. {
  99.     wait();
  100.     outp(0x43, 0xb6);               /* program the frequency */
  101.     outp(0x42, (int) (COUNT % 256));
  102.     outp(0x42, (int) (COUNT / 256));
  103.     outp(0x61, inp(0x61) | 3);      /* start the sound */
  104.     wait();
  105.     outp(0x61, inp(0x61) & ~3);     /* stop the sound  */
  106. }
  107.  
  108. /* -------- get the video mode and page from BIOS -------- */
  109. void videomode(void)
  110. {
  111.     regs.h.ah = 15;
  112.     int86(VIDEO, ®s, ®s);
  113.     video_mode = regs.h.al;
  114.     video_page = regs.x.bx;
  115.     video_page &= 0xff00;
  116.     video_mode &= 0x7f;
  117. }
  118.  
  119. /* ------ position the cursor ------ */
  120. void cursor(int x, int y)
  121. {
  122.     videomode();
  123.     regs.x.dx = ((y << 8) & 0xff00) + x;
  124.     regs.h.ah = SETCURSOR;
  125.     regs.x.bx = video_page;
  126.     int86(VIDEO, ®s, ®s);
  127. }
  128.  
  129. /* ------ get cursor shape and position ------ */
  130. static void near getcursor(void)
  131. {
  132.     videomode();
  133.     regs.h.ah = READCURSOR;
  134.     regs.x.bx = video_page;
  135.     int86(VIDEO, ®s, ®s);
  136. }
  137.  
  138. /* ------- get the current cursor position ------- */
  139. void curr_cursor(int *x, int *y)
  140. {
  141.     getcursor();
  142.     *x = regs.h.dl;
  143.     *y = regs.h.dh;
  144. }
  145.  
  146. /* ------ save the current cursor configuration ------ */
  147. void savecursor(void)
  148. {
  149.     if (cs < MAXSAVES)    {
  150.         getcursor();
  151.         cursorshape[cs] = regs.x.cx;
  152.         cursorpos[cs] = regs.x.dx;
  153.         cs++;
  154.     }
  155. }
  156.  
  157. /* ---- restore the saved cursor configuration ---- */
  158. void restorecursor(void)
  159. {
  160.     if (cs)    {
  161.         --cs;
  162.         videomode();
  163.         regs.x.dx = cursorpos[cs];
  164.         regs.h.ah = SETCURSOR;
  165.         regs.x.bx = video_page;
  166.         int86(VIDEO, ®s, ®s);
  167.         set_cursor_type(cursorshape[cs]);
  168.     }
  169. }
  170.  
  171. /* ------ make a normal cursor ------ */
  172. void normalcursor(void)
  173. {
  174.     set_cursor_type(0x0607);
  175. }
  176.  
  177. /* ------ hide the cursor ------ */
  178. void hidecursor(void)
  179. {
  180.     getcursor();
  181.     regs.h.ch |= HIDECURSOR;
  182.     regs.h.ah = SETCURSORTYPE;
  183.     int86(VIDEO, ®s, ®s);
  184. }
  185.  
  186. /* ------ unhide the cursor ------ */
  187. void unhidecursor(void)
  188. {
  189.     getcursor();
  190.     regs.h.ch &= ~HIDECURSOR;
  191.     regs.h.ah = SETCURSORTYPE;
  192.     int86(VIDEO, ®s, ®s);
  193. }
  194.  
  195. /* ---- use BIOS to set the cursor type ---- */
  196. void set_cursor_type(unsigned t)
  197. {
  198.     videomode();
  199.     regs.h.ah = SETCURSORTYPE;
  200.     regs.x.bx = video_page;
  201.     regs.x.cx = t;
  202.     int86(VIDEO, ®s, ®s);
  203. }
  204.  
  205. /* ---- test for EGA -------- */
  206. int isEGA(void)
  207. {
  208.     if (isVGA())
  209.         return 0;
  210.     regs.h.ah = 0x12;
  211.     regs.h.bl = 0x10;
  212.     int86(VIDEO, ®s, ®s);
  213.     return regs.h.bl != 0x10;
  214. }
  215.  
  216. /* ---- test for VGA -------- */
  217. int isVGA(void)
  218. {
  219.     regs.x.ax = 0x1a00;
  220.     int86(VIDEO, ®s, ®s);
  221.     return regs.h.al == 0x1a && regs.h.bl > 6;
  222. }
  223.  
  224. static void Scan350(void)
  225. {
  226.     regs.x.ax = 0x1201;
  227.     regs.h.bl = 0x30;
  228.     int86(VIDEO, ®s, ®s);
  229. }
  230.  
  231. static void Scan400(void)
  232. {
  233.     regs.x.ax = 0x1202;
  234.     regs.h.bl = 0x30;
  235.     int86(VIDEO, ®s, ®s);
  236. }
  237.  
  238. /* ---------- set 25 line mode ------- */
  239. void Set25(void)
  240. {
  241.     if (isVGA())    {
  242.         Scan400();
  243.         regs.x.ax = 0x1114;
  244.     }
  245.     else
  246.         regs.x.ax = 0x1111;
  247.     regs.x.bx = 0;
  248.     int86(VIDEO, ®s, ®s);
  249. }
  250.  
  251. /* ---------- set 43 line mode ------- */
  252. void Set43(void)
  253. {
  254.     if (isVGA())
  255.         Scan350();
  256.     regs.x.ax = 0x1112;
  257.     regs.x.bx = 0;
  258.     int86(VIDEO, ®s, ®s);
  259. }
  260.  
  261. /* ---------- set 50 line mode ------- */
  262. void Set50(void)
  263. {
  264.     if (isVGA())
  265.         Scan400();
  266.     regs.x.ax = 0x1112;
  267.     regs.x.bx = 0;
  268.     int86(VIDEO, ®s, ®s);
  269. }
  270.  
  271. /* ------ convert an Alt+ key to its letter equivalent ----- */
  272. int AltConvert(int c)
  273. {
  274.     int i, a = 0;
  275.     for (i = 0; i < 36; i++)
  276.         if (c == altconvert[i])
  277.             break;
  278.     if (i < 26)
  279.         a = 'a' + i;
  280.     else if (a < 36)
  281.         a = '0' + i - 26;
  282.     return a;
  283. }
  284.